﻿/// <reference path="../Core.ts" />

module Supernova {

    export class OptionsUI implements UI.ISwitchableUI {

        layout = {
            rootPanel: {
                top: 50, width: 400, bottom: 0,
                controls: {
                    welcomeTitle: {
                        top: 0, height: 30,
                        text: 'Options'
                    },
                    soundEffectsCheckBox: {
                        top: 40, height: 24,
                        text: 'Sound Effects',
                        onCheckedChanged: this.handleSoundEffectsCheckedChanged
                    },
                    showStarfieldCheckBox: {
                        top: 74, height: 24,
                        text: 'Display Starfield Background',
                        onCheckedChanged: this.handleShowStarfieldCheckedChanged
                    },
                    showOrbitsCheckBox: {
                        top: 108, height: 24,
                        text: 'Display Orbits',
                        onCheckedChanged: this.handleShowOrbitsCheckedChanged
                    },
                    showToolTipsCheckBox: {
                        top: 142, height: 24,
                        text: 'Display Hints',
                        onCheckedChanged: this.handleShowToolTipsCheckedChanged
                    },
                    logIncomingPacketsCheckBox: {
                        top: 176, height: 24,
                        text: 'Log Incoming Packets',
                        onCheckedChanged: this.handleLogIncomingPacketsCheckedChanged
                    },
                    okButton: {
                        left: 0, top: 230, width: 100, height: 40,
                        text: 'OK',
                        onClick: this.handleOKButtonClicked
                    },
                    cancelButton: {
                        left: 110, top: 230, width: 100, height: 40,
                        text: 'Cancel',
                        onClick: this.handleCancelButtonClicked
                    },
                    exitGameButton: {
                        bottom: 200, height: 40,
                        text: 'Exit Game',
                        onClick: this.handleExitGameButtonClicked
                    }
                }
            }
        }

        private returnUI: UI.ISwitchableUI = null;

        public root: UI.UIControl = null;

        constructor() {
            this.root = UI.compile(this.layout)[0];
        }

        show(oldUI: UI.ISwitchableUI) {
            optionsUI.returnUI = oldUI;
            (UI.getControlByName<UI.CheckBox>(optionsUI.root, 'soundEffectsCheckBox')).isChecked = Options.soundEffects;
            (UI.getControlByName<UI.CheckBox>(optionsUI.root, 'showStarfieldCheckBox')).isChecked = Options.showStarfield;
            (UI.getControlByName<UI.CheckBox>(optionsUI.root, 'showOrbitsCheckBox')).isChecked = Options.showOrbitLines;
            (UI.getControlByName<UI.CheckBox>(optionsUI.root, 'showToolTipsCheckBox')).isChecked = Options.showToolTips;
            (UI.getControlByName<UI.CheckBox>(optionsUI.root, 'logIncomingPacketsCheckBox')).isChecked = Options.logIncomingPackets;
            (UI.getControlByName<UI.Button>(optionsUI.root, 'exitGameButton')).visible = galaxy !== null;
        }

        hide() {
        }

        handleSoundEffectsCheckedChanged(sender: UI.UIControl, e: UI.InputArgs) {
            Options.soundEffects = (<UI.CheckBox>sender).isChecked;
            Sounds.play(Sounds.items.blip);
        }

        handleShowStarfieldCheckedChanged(sender: UI.UIControl, e: UI.InputArgs) {
            Options.showStarfield = (<UI.CheckBox>sender).isChecked;
        }

        handleShowOrbitsCheckedChanged(sender: UI.UIControl, e: UI.InputArgs) {
            Options.showOrbitLines = (<UI.CheckBox>sender).isChecked;
        }

        handleShowToolTipsCheckedChanged(sender: UI.UIControl, e: UI.InputArgs) {
            Options.showToolTips = (<UI.CheckBox>sender).isChecked;
        }

        handleLogIncomingPacketsCheckedChanged(sender: UI.UIControl, e: UI.InputArgs) {
            Options.logIncomingPackets = (<UI.CheckBox>sender).isChecked;
        }

        handleOKButtonClicked(sender: UI.UIControl, e: UI.InputArgs) {
            Options.save();
            UI.swap(optionsUI.returnUI, UI.Bounds.create(canvas));
        }

        handleCancelButtonClicked(sender: UI.UIControl, e: UI.InputArgs) {
            Options.load();
            UI.swap(optionsUI.returnUI, UI.Bounds.create(canvas));
        }

        handleExitGameButtonClicked(sender: UI.UIControl, e: UI.InputArgs) {
            resetToMainMenu();
        }

    }

}